Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to create thread in UNUX

Posted on 2000-02-29
5
Medium Priority
?
189 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 400 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month10 days, 1 hour left to enroll

926 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