C语言如何像Java一样编写多线程来完成特定任务?可以像objective-c那样完成吗?
我们如何创建多线程或进程?使用C语言跨平台?并在需要时关闭线程。一次编写即可部署Linux/Windows/Mac/Android/MeeGo等平台。
java 中的示例: runnable = new Mytest();线程=新线程(可运行);
Step 1:
runnable = new Mytest();
thread = new Thread(runnable);
Step 2:
public class Mytest implements Runnable
{
private static volatile boolean running = true
public void run()
{
while(running)
{ // do stuff }
}
public void start() { running = true; }
public void stop() { running = false;}
}
跟进:
第1步:vim omp_hello.c ( https:/ /computing.llnl.gov/tutorials/openMP/exercise.html )
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
int nthreads, tid;
#pragma omp parallel private(nthreads, tid)
{
/* Obtain thread number */
tid = omp_get_thread_num();
printf("Hello World from thread = %d\n", tid);
/* Only master thread does this */
if (tid == 0)
{
nthreads = omp_get_num_threads();
printf("Number of threads = %d\n", nthreads);
}
} /* All threads join master thread and disband */
}
步骤 2: $ gcc -fopenmp omp_hello.c -o hello
步骤 3: $ setenv OMP_NUM_THREADS 4
步骤 4:$ ./hello
Hello World from thread = 0
Number of threads = 4
Hello World from thread = 3
Hello World from thread = 1
Hello World from thread = 2
完成。有用!
How do we create multi thread or process? For cross platform using C language? And close the thread when it requires. Write once and deploy platforms such as Linux/Windows/Mac/Android/MeeGo.
Example in java: runnable = new Mytest(); thread = new Thread(runnable);
Step 1:
runnable = new Mytest();
thread = new Thread(runnable);
Step 2:
public class Mytest implements Runnable
{
private static volatile boolean running = true
public void run()
{
while(running)
{ // do stuff }
}
public void start() { running = true; }
public void stop() { running = false;}
}
Follow up:
Step 1: vim omp_hello.c ( https://computing.llnl.gov/tutorials/openMP/exercise.html )
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
int nthreads, tid;
#pragma omp parallel private(nthreads, tid)
{
/* Obtain thread number */
tid = omp_get_thread_num();
printf("Hello World from thread = %d\n", tid);
/* Only master thread does this */
if (tid == 0)
{
nthreads = omp_get_num_threads();
printf("Number of threads = %d\n", nthreads);
}
} /* All threads join master thread and disband */
}
Step 2: $ gcc -fopenmp omp_hello.c -o hello
Step 3: $ setenv OMP_NUM_THREADS 4
Step 4: $ ./hello
Hello World from thread = 0
Number of threads = 4
Hello World from thread = 3
Hello World from thread = 1
Hello World from thread = 2
Done. it Works!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
C语言中没有线程的概念。大多数平台都有自己特定的线程库或API(显然有些平台根本不支持线程)。
但是,存在诸如 OpenMP 之类的第 3 方库,它们在一定程度上提供了平台无关的线程。
There is no concept of threading in the C language. Most platforms have their own specific libraries or API for threading (obviously some platforms can't support threading at all).
However, 3rd-party libraries such as OpenMP exist, which offer platform-independent threading, to an extent.
在 C++ 中,您需要使用 Boost Thread 库 - 它是跨平台的,易于使用,而且非常可靠。我不知道有类似的 C 库。
线程纯粹是 C 和 C++ 中的库问题,特别是因为它们在每个平台上的实现方式都不同。
In C++, you will want to use the Boost Thread library - it's cross-platform, easy to use, and really solid. I don't know of a similar library for C.
Threads are purely a library issue in C and C++, specifically because they're implemented differently on every platform.
对于C语言,您可以使用pthread,但是对于跨平台开发,这取决于您解决具体问题对于每个操作系统。
With C language you can use pthread but for what concerning croossplatform developing, it's up to you resolving specific issue for every operating system.
wxWidget 是多平台的(Linux/Windows/Mac 上的 C++),并且有它的特殊线程类:wxThread(多平台)。顺便说一下,GUI 也是多平台的。
我不明白如何使用不同的语言部署一次(android 是 Java,你想要 c++?)
wxWidget is multiplatform(C++ on Linux/Windows/Mac) and has it's special thread class : wxThread (multiplatform). GUI is also multiplatform by the way.
I don't see how you can deploy once using different languages (android is Java and you want c++?)