草庐IT

关于具有多个参数的进程的 c:pthread

codeneng 2023-03-28 原文

pthread for processes with more than one parameter

我目前正在创建一个使用线程处理 BMP 图像的程序。问题是......我知道 pthread 使用函数的签名作为 arg 4......但是如果函数需要超过 1 个参数,我怎么能创建一个线程呢?

这是函数所需的结构:

1
2
3
4
5
6
7
typedef struct {
HEADER header;
INFOHEADER infoheader;
PIXEL *pixel;
} IMAGE;

IMAGE imagenfte,imagendst;

功能代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
void *processBMP(IMAGE *imagefte, IMAGE *imagedst)
{
int i,j;
int count=0;
PIXEL *pfte,*pdst;
PIXEL *v0,*v1,*v2,*v3,*v4,*v5,*v6,*v7;
int imageRows,imageCols;
memcpy(imagedst,imagefte,sizeof(IMAGE)-sizeof(PIXEL *));
imageRows = imagefte->infoheader.rows;
imageCols = imagefte->infoheader.cols;
imagedst->pixel=(PIXEL *)malloc(sizeof(PIXEL)*imageRows*imageCols);
for(i=1;i<imageRows-1;i++){
for(j=1;j<imageCols-1;j++)
{
pfte=imagefte->pixel+imageCols*i+j;
v0=pfte-imageCols-1;
v1=pfte-imageCols;
v2=pfte-imageCols+1;
v3=pfte-1;
v4=pfte+1;
v5=pfte+imageCols-1;
v6=pfte+imageCols;
v7=pfte+imageCols+1;
pdst=imagedst->pixel+imageCols*i+j;
...
}

...
int main()
{
int res;

///////Variables a utilizar en Hilo///////
int procBMP_t;  //Variable entera para la creaci?3n de los hilos
pthread_t proc_t;  //Hilo para el procesamiento del BMP
//////////////////////////////////////////

...
procBMP_t = pthread_create(&proc_t, NULL, processBMP, (void *) &imgsSENT);  //Hilo para el procesamiento de imagenes

我试着用这个来解决它:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct threadImgs{   //Estructura para enviar ambas imagenes como argumento del thread
IMAGE fuente;
IMAGE destino;
};

struct threadImgs imgsSENT;


void *processBMP(void *imgs)
{

struct threadImgs* imagen = (struct threadImgs *) imgs;

IMAGE *imagefte = imagen->fuente;
IMAGE *imagedst = imagen->destino;
....
}

但它并没有真正的帮助,甚至无法编译由于"初始化类型结构错误时不兼容的类型"

任何帮助 D:???

  • 传递参数数组或用包含两个字段的结构package


您将两个参数package到一个结构中的方法是要走的路。

但请注意类型:

1
2
3
4
5
struct threadImgs
{  
  IMAGE fuente;
  IMAGE destino;
};

上面的结构定义它的元素是 IMAGE.

的实例

您的线程函数尝试访问指向 IMAGE:

的指针

1
2
3
4
5
6
7
8
void *processBMP(void *imgs)
{
  struct threadImgs* imagen = (struct threadImgs *) imgs;

  IMAGE *imagefte = imagen->fuente;
  IMAGE *imagedst = imagen->destino;

  ...

所以要么改变结构体将其元素定义为指针:

1
2
3
4
5
struct threadImgs
{  
  IMAGE * fuente;
  IMAGE * destino;
};

或者改变线程函数不拉指针:

1
2
3
4
5
6
7
8
void *processBMP(void *imgs)
{
  struct threadImgs * imagen = (struct threadImgs *) imgs;

  IMAGE imagefte = imagen->fuente;
  IMAGE imagedst = imagen->destino;

  ...

  • 或者只是做 IMAGE *imagefte = &imagen->fuente;
  • 我知道我不应该使用评论来感谢用户购买... @alk 这非常有效,非常感谢您回答我的(也许是愚蠢的)问题!

有关关于具有多个参数的进程的 c:pthread的更多相关文章

  1. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  2. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

  3. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  4. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  5. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  6. ruby - 在 jRuby 中使用 'fork' 生成进程的替代方案? - 2

    在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',

  7. ruby - RSpec - 使用测试替身作为 block 参数 - 2

    我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere

  8. ruby - 通过 ruby​​ 进程共享变量 - 2

    我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是

  9. ruby - 如何在 Ruby 中拆分参数字符串 Bash 样式? - 2

    我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"

  10. ruby - 多个属性的 update_column 方法 - 2

    我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2

随机推荐