Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Threading in C

Tags:

c

I want to create a thread in C so that the thread automatically call after two seconds. I am using Visual Studio and Windows platform for development.

How do I get started?

like image 509
Siddiqui Avatar asked Dec 29 '09 07:12

Siddiqui


People also ask

What is threading in C?

A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution. C does not contain any built-in support for multithreaded applications.

How do we create a thread in C?

In main(), we declare a variable called thread_id, which is of type pthread_t, which is an integer used to identify the thread in the system. After declaring thread_id, we call pthread_create() function to create a thread.

Do we have threads in C?

Yes you can use threads with C and there are various libraries you can use to do this, pthreads being one of them.

What is threading in programming?

With computer programming, a thread is a small set of instructions designed to be scheduled and executed by the CPU independently of the parent process. For example, a program may have an open thread waiting for a specific event to occur or running a separate job, allowing the main program to perform other tasks.


3 Answers

You are going to need to use OS specific libraries to do threading. On Posix, you will want to look into pthreads (and specifically pthread_create). On Windows, you'll want CreateThread or _beginthreadex.

like image 114
R Samuel Klatchko Avatar answered Sep 29 '22 15:09

R Samuel Klatchko


Multithreading in C is platform dependent. You need to use external libraries corresponding to different platforms.

Read about:

Multithreading in C, POSIX style and Multithreading with C and Win32

like image 39
Prasoon Saurav Avatar answered Sep 29 '22 13:09

Prasoon Saurav


C doesn't have built in threading facilities; you will have to use your OS services to create a thread.

For windows use CreateThread function.

like image 35
Alon Avatar answered Sep 29 '22 15:09

Alon