Solved

How to create thread in UNUX

Posted on 2000-02-29
5
176 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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 fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

756 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