Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to create thread in UNUX

Posted on 2000-02-29
5
Medium Priority
?
187 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

660 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