Solved

How to create thread in UNUX

Posted on 2000-02-29
5
173 Views
Last Modified: 2010-04-21
I would like to know how to create threads in UNIX systems using C/C++ with out nonstandard libraries and to obtain some example programs. How to run, stop and kill threads from parent process?
0
Comment
Question by:MaxZ
5 Comments
 
LVL 1

Accepted Solution

by:
hansendc earned 100 total points
ID: 2570088
Here is a fun little program that should get you started.  It uses the POSIX thread library, which is standard.  Do a "man pthread_create" on a UNIX system to get some good info.


#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

int count;

void increment( int ntimes )
{
        for ( int i = 0; i < ntimes; i++ ) {
                int c;

                c = count;
                c = c + 1;

                count = c;
        }
}

int main( int argc, char ** argv )
{
        int n = 10000000;
        pthread_t t1, t2;
        pthread_attr_t attr;

        pthread_attr_init( &attr );
        pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);

        printf("Start Test. Final count should be %d\n", 2 * n );

        // Create threads
        pthread_create( &t1, &attr, (void * (*)(void *)) increment,
                        (void *) n);

        pthread_create( &t2, &attr, (void * (*)(void *)) increment,
                        (void *) n);

        // Wait until threads are done
        pthread_join( t1, NULL );
        pthread_join( t2, NULL );

        if ( count != 2 * n ) {
                printf("\n****** Error. Final count is %d\n", count );
                printf("****** It should be %d\n", 2 * n );
        }
        else {
                printf("\n>>>>>> O.K. Final count is %d\n", count );
        }
}
0
 
LVL 2

Expert Comment

by:curri
ID: 2585572
There is NO support for threads in standard C or standard C++. You can find several different 'standards' for threads (and probably the best is the posix one that is mentioned above)
0
 
LVL 1

Expert Comment

by:hansendc
ID: 2585630
While correct about not having it be a part of the language in C++ or C, neither is printf.  
MaxZ, has your question been answered?
0
 

Expert Comment

by:ramaswamys
ID: 2586672
You can find answers to your questions and a whole lot of other useful links at the  'The LinuxThreads Library' site (http://pauillac.inria.fr/~xleroy/linuxthreads/ ).

Ramaswamy
0
 

Author Comment

by:MaxZ
ID: 2586706
Thank you for your answer. It was exactly what I needed.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Debian 3 - initrd rebuild 3 69
usage of %{name} in rpm spec file 4 49
what does "puppet agent -t" do ? 2 191
WiFi Router device supports GPON! 3 31
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now